﻿using BLL;
using DevExpress.XtraEditors;
using NursingFormSystem;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Xml;

namespace NursingDocumentInfoSystem
{
    public partial class NursingSummarySelForm3 : Form
    {
        
        public string str_ReturnSummary = "";//返回选取的病情摘要
        public string TempStr = "";
        public int num = 0;
        public ArrayList itemlist = new ArrayList();
        public ArrayList itemlist2 = new ArrayList();
        string patientId = "";
        public NursingSummarySelForm3(string str, string patientid)
        {
            InitializeComponent();
            str_ReturnSummary = str;
            patientId = patientid;

            //初始化两个combox
            GetDDLSource();

            Create(str);
        }

        #region 获取xml下拉框数据源
        /// <summary>
        /// 输入选择、途径
        /// </summary>
        private void GetDDLSource()
        {
            if (patientId == null) return;
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc = XmlUtil.GetItems();
            XmlNodeList list = xmlDoc.GetElementsByTagName("Items");
            if (list == null || list.Count == 0) { return; }
            for (int i = 0; i < list.Count; i++)
            {
                int listCount = list[i].ChildNodes.Count;
                if (listCount > 0)//判断是否有子项
                {
                    string itemsKey = list[i].Attributes["key"].Value;//<Items key="卡类型" default="">取到Items节点中key的值
                    for (int j = 0; j < listCount; j++)
                    {
                        string itemKey = list[i].ChildNodes[j].Attributes["key"].Value;//给需要的下拉框赋值
                        if (itemsKey.Equals("输入选择"))
                        {
                            itemlist.Add(itemKey);//<Item key="医保卡" value="1"/>
                        }
                        if (itemsKey.Equals("途径"))
                        {
                            itemlist2.Add(itemKey);
                        }
                    }
                }
            }
        }
        #endregion
        void Create(string str) //构造
        {
            
            string[] temp = Regex.Split(str, "#", RegexOptions.IgnoreCase);

            string[] tempname = Regex.Split(temp[0], ",", RegexOptions.IgnoreCase);
            string[] tempnum = Regex.Split(temp[1], ",", RegexOptions.IgnoreCase);
            string[] tempway = Regex.Split(temp[2], ",", RegexOptions.IgnoreCase);
            num = tempname.Length;
            if (num > 0)
            {
                for (int i = 0; i < num ; i++)
                {
                    CheckEdit del = new CheckEdit();
                   
                    del.Text = "启用";
                    del.Tag = "1";
                    del.Location = new Point(20, 10 + 21 * (i + 2));
                    del.Name = string.Format(i.ToString());
                    del.Width = 50;
                    del.Checked = true;
                    Controls.Add(del);
                    ComboBoxEx com = new ComboBoxEx();
                    com.Location = new Point(100, 10 + 21 * (i + 2));
                    com.Name = string.Format(i.ToString() + "com");
                    for (int j = 0; j < itemlist.Count; j++)
                    {
                        com.Items.Add(itemlist[j]);
                    }
                    com.Text = tempname[i];
                    com.Click += com_Click;
                    Controls.Add(com);
                    TextBox tex = new TextBox();
                    tex.Location = new Point(250, 10 + 21 * (i + 2));
                    tex.Name = string.Format(i.ToString() + "tex");
                    tex.Width = 50;
                    if (i < tempnum.Length)
                    {
                        tex.Text = tempnum[i].Trim().TrimEnd('l').TrimEnd('m');
                    }
                    else
                        tex.Text = "";
                    Controls.Add(tex);
                    Label lab = new Label();
                    lab.Location = new Point(300, 12 + 21 * (i + 2));
                    lab.Text = "ml";
                    lab.Name = string.Format(i.ToString() + "lab");
                    lab.Width = 20;
                    Controls.Add(lab);
                    ComboBoxEx com2 = new ComboBoxEx();
                    com2.Location = new Point(330, 10 + 21 * (i + 2));
                    com2.Name = string.Format(i.ToString() + "com2");
                    for (int j = 0; j < itemlist2.Count; j++)
                    {
                        com2.Items.Add(itemlist2[j]);
                    }
                    if (i < tempway.Length)
                    {
                         com2.Text = tempway[i];
                    }
                    else
                         com2.Text = "";
                    com2.Click += com_Click;
                    Controls.Add(com2);

                    if (num > 13)
                    {
                        this.Height = Height + 30;
                        simpleButton1.Location = new Point(simpleButton1.Location.X, simpleButton1.Location.Y + 30);
                        simpleButton2.Location = new Point(simpleButton2.Location.X, simpleButton2.Location.Y + 30);
                        AddButton.Location = new Point(AddButton.Location.X, AddButton.Location.Y + 30);
                    }
                }
            }
        }

        void com_Click(object sender, EventArgs e)
        {
            if ((sender as ComboBoxEx)!=null)
            {
                ComboBoxEx cbx = (sender as ComboBoxEx);
                cbx.DroppedDown = true;
            }
        }

        void Save()  //保存
        {
            ArrayList NameList = new ArrayList();
            ArrayList NumList = new ArrayList();
            ArrayList WayList = new ArrayList();
            int countCheck = 0;//统计选中个数
            for (int i = 0; i < num; i++)
            {
                if (true == ((CheckEdit)(Controls[i.ToString()])).Checked)
                {
                    countCheck++;
                    string nameVal = Controls[i.ToString() + "com"].Text;
                    nameVal = !string.IsNullOrEmpty(nameVal) ? nameVal : " ";
                    NameList.Add(nameVal);

                    string numVal = Controls[i.ToString() + "tex"].Text;
                    numVal = !string.IsNullOrEmpty(numVal) ? numVal : " ";
                    NumList.Add(numVal);

                    string wayVal = Controls[i.ToString() + "com2"].Text;
                    wayVal = !string.IsNullOrEmpty(wayVal) ? wayVal : " ";
                    WayList.Add(wayVal);
                }
            }
            if (countCheck == 0)
            {
                TempStr = "##";
                XtraMessageBox.Show("没有勾选对应输入流体及食物");
            }
            else
            {
                for (int j = 0; j < NameList.Count; j++)
                {
                    TempStr += NameList[j].ToString();
                    TempStr += ",";
                }
                TempStr = TempStr.Substring(0, TempStr.Length - 1);
                TempStr += "#";
                for (int j = 0; j < NumList.Count; j++)
                {
                    TempStr += NumList[j].ToString();
                    TempStr += ",";
                }
                TempStr = TempStr.Substring(0, TempStr.Length - 1);
                TempStr += "#";
                for (int j = 0; j < WayList.Count; j++)
                {
                    TempStr += WayList[j].ToString();
                    TempStr += ",";
                }
                TempStr = TempStr.Substring(0, TempStr.Length - 1);
            }
        }

  

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            Save();
            DialogResult = DialogResult.OK;
            Close();

        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.No;
            Close();
        }

        private void AddButton_Click(object sender, EventArgs e)
        {
            CheckEdit del = new CheckEdit();
        
            del.Text = "启用";
            del.Location = new Point(20, 10 + 21 * (num + 3));
            del.Name = string.Format(num.ToString());
            del.Width = 50;
            del.Checked = true;
            Controls.Add(del);
            ComboBoxEx com = new ComboBoxEx();
            com.Location = new Point(100, 10 + 21 * (num + 3));
            com.Name = string.Format(num.ToString()+"com");
            for(int i = 0; i < itemlist.Count; i++)
            {
                com.Items.Add(itemlist[i]);
            }
            com.Click += com_Click;
            Controls.Add(com);
            TextBox tex = new TextBox();
            tex.Location = new Point(250, 10 + 21 * (num + 3));
            tex.Name = string.Format(num.ToString() + "tex");
            tex.Width = 50;
            Controls.Add(tex);
            Label lab = new Label();
            lab.Location = new Point(300, 12 + 21 * (num + 3));
            lab.Text = "ml";
            lab.Name = string.Format(num.ToString() + "lab");
            lab.Width = 20;
            Controls.Add(lab);
            ComboBoxEx com2 = new ComboBoxEx();
            com2.Location = new Point(330, 10 + 21 * (num + 3));
            com2.Name = string.Format(num.ToString() + "com2");
            for (int i = 0; i < itemlist2.Count; i++)
            {
                com2.Items.Add(itemlist2[i]);
            }
            com2.Click += com_Click;
            Controls.Add(com2);
            num++;
            if(num > 13)
            {
                this.Height = Height + 30;
                simpleButton1.Location = new Point(simpleButton1.Location.X, simpleButton1.Location.Y + 30);
                simpleButton2.Location = new Point(simpleButton2.Location.X, simpleButton2.Location.Y + 30);
                AddButton.Location = new Point(AddButton.Location.X, AddButton.Location.Y + 30);
            }
        }

       
    }
}
